<?php

/* ----------------------------------------------
  Caloris: Module Permissions

  $Id$
  $Author$

  Editor práv uživatelů
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'permissions');


if ($_REQUEST["name"] == 'permissions_insert') {
    if (Caloris\ACL::authorize("permissions") == 1) {
        $xml = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');

        for ($a = 0; $a < count($xml->permission); $a++) {
            if ($xml->permission[$a]->name == $_POST["title"]) {
                header('Location: ' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/permissions/index.php?msg=' . urlencode('Název oprávnění již existuje'));
                exit();
            }
        }

        $xml->data->id = $xml->data->id + 1;
        $xml->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');

        $doc = new DOMDOcument;
        $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
        $xpath = new DOMXpath($doc);

        $qry = '/core';

        foreach ($xpath->query($qry) as $node) {
            $foo = $doc->createElement('permission');
            $node->appendChild($foo);

            $idAttribute = $doc->createAttribute("id");
            $foo->appendChild($idAttribute);

            $priceValue = $doc->createTextNode(($xml->data->id));
            $idAttribute->appendChild($priceValue);

            $bar = $doc->createElement("name", $_POST["title"]);
            $foo->appendChild($bar);

            $bazz = $doc->createElement("types", "");
            $foo->appendChild($bazz);

            $nodePermissions = $doc->createElement("permissions", 0);
            $bazz->appendChild($nodePermissions);

            $nodeExtensions = $doc->createElement("extensions", $_POST["extensions"]);
            $bazz->appendChild($nodeExtensions);

            $nodeTemplates = $doc->createElement("templates", $_POST["templates"]);
            $bazz->appendChild($nodeTemplates);

            $nodeSettings = $doc->createElement("settings", $_POST["settings"]);
            $bazz->appendChild($nodeSettings);

            $nodeModules = $doc->createElement("modules", $_POST["modules"]);
            $bazz->appendChild($nodeModules);

            $nodeUsers = $doc->createElement("users", $_POST["users"]);
            $bazz->appendChild($nodeUsers);

            $nodeSystem = $doc->createElement("system", 0);
            $foo->appendChild($nodeSystem);
        }

        $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');

        if (class_exists('tidy')) {
            $tidy_options = array(
                'input-xml' => true,
                'output-xml' => true,
                'indent' => true,
                'wrap' => false,
            );

            $tidy = new tidy();
            $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml', $tidy_options, 'utf8');
            $tidy->cleanRepair();

            $xml = simplexml_load_string($tidy);
            $xml->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
        }
    } else {
        header('Location: ' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/permissions/index.php?msg=' . urlencode('Musíte mít oprávnění Administrator, abyste mohli měnit toto nastavení'));
        exit();
    }

    header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/permissions/index.php");
    exit();
} elseif ($_REQUEST["name"] == 'permissionsDelete') {
    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');

    $xpath = new DOMXpath($doc);
    $qry = '//permission[@id="' . $_REQUEST["id"] . '"]';

    foreach ($xpath->query($qry) as $node) {
        $node->parentNode->removeChild($node);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
    if (class_exists('tidy')) {
        $tidy_options = array(
            'input-xml' => true,
            'output-xml' => true,
            'indent' => true,
            'wrap' => false,
        );

        $tidy = new tidy();
        $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml', $tidy_options, 'utf8');
        $tidy->cleanRepair();

        $xml = simplexml_load_string($tidy);
        $xml->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
        exit();
    }
}

function chooseperm($perm) {
    if ($perm == 1) {
        $content = 'Ano';
    } else {
        $content = 'Ne';
    }

    return $content;
}

$content = '
<h1>Oprávnění</h1>

<div style="color: red; font-size: 0.90em; height: 25px;" id="msg">' . $_REQUEST["msg"] . '</div>' . PHP_EOL;

$xmlPermissionsF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/permissions.xml');
$xmlPermissions = $xmlPermissionsF->xpath('//permission');

// sort by name
function sort_trees($t1, $t2) {
    return strcoll($t1->name, $t2->name);
}

usort($xmlPermissions, 'sort_trees');


if (count($xmlPermissions) > 0) {
    $content .= '
<table class="table table-striped">
    <tr>
        <th style="width: 20px;">&nbsp;</th>
        <th>Popis</th>
        <th>Extenze</th>
        <th>Moduly</th>
        <th>Uživatelé</th>
        <th>Nastavení</th>
        <th>Stránky</th>
    </tr>
';

    for ($a = 0; $a < count($xmlPermissions); $a++) {
        $content .= '
    <tr id="trbl_' . $a . '">
        <td style="width: 20px;">
';
        if ($xmlPermissions[$a]->system == 0) {
            $content .= '<form id="formshw_' . $a . '" action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/permissions/index.php" name="trbl_' . $a . '" method="post" onsubmit="return false;">
<input type="hidden" name="name" value="permissionsDelete"  />
<input type="hidden" name="id" value="' . $xmlPermissions[$a]["id"] . '"  />
<input type="submit" id="formx_' . $a . '" value="Smazat" class="btn btn-danger" />
</form';
        }

        $content .= '
        </td>
        <td>' . $xmlPermissions[$a]->name . '</td>
        <td>' . chooseperm($xmlPermissions[$a]->types->extensions) . '</td>
        <td>' . chooseperm($xmlPermissions[$a]->types->modules) . '</td>
        <td>' . chooseperm($xmlPermissions[$a]->types->users) . '</td>
        <td>' . chooseperm($xmlPermissions[$a]->types->settings) . '</td>
        <td>' . chooseperm($xmlPermissions[$a]->types->templates) . '</td>
    </tr>';
    }

    $content .= '</table>';
}

$content .= '

<h2>Nové oprávnění</h2>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/permissions/index.php" method="post" class="form-horizontal">
<input type="hidden" name="name" value="permissions_insert" />
<div class="control-group">
<label class="control-label">Název oprávnění</label>
<div class="controls"><input type="text" name="title" style="width: 250px;" /></div>
</div>

<div class="control-group">
<label class="control-label">Uživatelé</label>
<div class="controls">' . Caloris\Tools::answerio(0, 'users', 0) . '</div>
</div>

<div class="control-group">
<label class="control-label">Nastavení</label>
<div class="controls">' . Caloris\Tools::answerio(0, 'settings', 0) . '</div>
</div>

<div class="control-group">
<label class="control-label">Extenze</label>
<div class="controls">' . Caloris\Tools::answerio(0, 'extensions', 0) . '</div>
</div>

<div class="control-group">
<label class="control-label">Stránky</label>
<div class="controls">' . Caloris\Tools::answerio(0, 'templates', 0) . '</div>
</div>

<div class="control-group">
<label class="control-label">Moduly</label>
<div class="controls">' . Caloris\Tools::answerio(0, 'modules', 0) . '</div>
</div>

<div class="control-group">
<div class="controls"><button type="submit" class="btn btn-primary">Vytvořit</button></div>
</div>
</form>' . PHP_EOL;

$data["title"] = 'Oprávnění';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');